package com.duleme.servlet;

import com.duleme.common.EncodeUtils;
import com.duleme.dao.UserDao;
import com.duleme.entity.Captcha;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet(name = "UserRegisterServlet",urlPatterns = "/User/Register")
public class UserRegisterServlet extends HttpServlet {
    UserDao userDao = new UserDao();

    public void  check(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userAccount = request.getParameter("userAccount");
        if(userDao.checkAccountValid(userAccount)){
            PrintWriter writer = response.getWriter();
            String captcha = request.getParameter("captcha");
            Captcha checkCaptcha = (Captcha)request.getSession().getAttribute("phoneCaptcha");
            if (checkCaptcha == null){
                writer.print(2);
                System.out.println("验证码"+":error");
                writer.close();
            }else{
                System.out.println(userAccount);
                System.out.println(captcha);
                System.out.println(checkCaptcha.getCode());
                long nh = 1000 * 60 * 60;
                if (!userAccount.equals(checkCaptcha.getUserAccount())){
                    System.out.println("账号"+":error");
                    writer.print(1);//账号不匹配
                    writer.close();
                }else {
                    if (!captcha.equals(checkCaptcha.getCode())){
                        writer.print(2);//验证码不匹配
                        System.out.println("验证码"+":error");
                        writer.close();
                    }
                    else {
                        if (Math.abs(new Date().getTime()-checkCaptcha.getCreateTime().getTime())/nh>checkCaptcha.getExpire()){
                            writer.print(3);//超时
                            System.out.println("时间"+":error");
                            writer.close();
                        }
                        else {
                            writer.print(0);
                            System.out.println("success");
                            writer.close();
                        }
                    }
                }
            }
        }
        else {
            response.getWriter().print(4);
            System.out.println("此账号已被注册");
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EncodeUtils.setEncoding(request,response);
        check(request,response);
        //String userAccount=request.getParameter("userAccount");
        /*String password=request.getParameter("userPassword");
        String userName = request.getParameter("userName");
        String identify = request.getParameter("identify");
        UserService userService=new UserServiceImpl();
        System.out.print(userAccount+":"+password);
        User user = new User();
        user.setUserAccount(userAccount);
        user.setUserPwd(password);
        user.setIdentity(Integer.valueOf(identify));
        user.setUserName(userName);
        user.setCreateDate(new Date());
        Boolean register = userService.register(user);
        PrintWriter writer=response.getWriter();
        if(register){
            writer.print("1");
        }else{
            writer.print("0");
        }
        writer.close();*/


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("huode");
    }
}
